﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class ProductPictureRepository : Data.Repository.IProductPictureRepository
    {
        private LinqToECommerceDBDataContext _dbContext;

        #region IProductPictureRepository Members

        public int CreateNewProductPicture(Data.Entity.ProductPicture theNewProductPicture)
        {
            if (theNewProductPicture == null)
            {
                return 0;
            }

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ProductPicture_Create(theNewProductPicture.ProductID, theNewProductPicture.PictureID, theNewProductPicture.DisplayOrder);
			}

			return result;
		}

		/// <summary>
		/// delete product's picture
		/// </summary>
		/// <param name="theProductPicture"></param>
		/// <returns>0: failed 1: successed</returns>
		public int DeleteProductPicture(Data.Entity.ProductPicture theProductPicture)
		{
			if(theProductPicture == null || theProductPicture.ProductID <= 0 || theProductPicture.PictureID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ProductPicture_Delete(theProductPicture.PictureID, theProductPicture.ProductID);
			}

			return result;
		}

		public IList<Data.Entity.ProductPicture> GetProductPictureByProduct(int productID)
		{
			if(productID <= 0)
			{
				return null;
			}

			List<ECommerce_GetProductPicture_By_ProductResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetProductPicture_By_Product(productID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ProductPicture>();

				foreach(var pp in lstData)
				{
					var theEntity = new Data.Entity.ProductPicture()
					{
						DisplayOrder = pp.DisplayOrder,
						PictureID = pp.PictureID,
						ProductID = pp.ProductID,
						ProductPictureID = pp.ProductPictureID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int UpdateProductPicture(Data.Entity.ProductPicture theProductPicture)
		{
			if(theProductPicture == null || theProductPicture.ProductPictureID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbProductPicture = (from rp in _dbContext.ProductPictures
										where rp.ProductPictureID == theProductPicture.ProductPictureID
										select rp).FirstOrDefault();

				if(dbProductPicture == null)
				{
					result = 0;
				}
				else
				{
					dbProductPicture.DisplayOrder = theProductPicture.DisplayOrder;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;
		}

		#endregion
	}
}
